#include "comm.hpp"
#include "log.hpp"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    Init init;  // 创建管道
    Log log(Classfile);    // 启动日志

    //log.outMethod(Screan);     // 将日志打印到屏幕
    //log.outMethod(Onefile);    // 将日志打印到logtxt文件中
    //log.outMethod(Classfile);  // 将日志分类打印到logtxt.$(类型)文件中

    // 打开管道
    int fd = open(FIFO_FILE, O_RDONLY);
    if (fd < 0)
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_DELETE_ERR);
    }

    log(Info,    "this is a test. error string: %s, error code: %d", strerror(errno), errno);
    log(Debug,   "this is a test. error string: %s, error code: %d", strerror(errno), errno);
    log(Warning, "this is a test. error string: %s, error code: %d", strerror(errno), errno);
    log(Error,   "this is a test. error string: %s, error code: %d", strerror(errno), errno);
    log(Fatal,   "this is a test. error string: %s, error code: %d", strerror(errno), errno);

    // 开始通信
    while (true)
    {
        char buffer[BUFFERSIZE];  // 接收字符串数据
        buffer[0] = 0;

        int RedNum = read(fd, buffer, sizeof(buffer));
        if (RedNum > 0) {
            buffer[RedNum] = 0;
            std::cout << "client say [" << buffer << "]" << std::endl;
        }
        else if (RedNum == 0)
        {
            std::cout << "client quit" << std::endl;
            break;
        }
        else break;
    }
    close(fd);

    // 删除管道，Init自行处理
  

    return 0;
}